package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

/**
 * 支付相关
 * Created by MartinKent on 2016/9/13.
 */
public class PaymentService extends BasicService<JSONArray> {
//    获取支付类型
    public void getPaymentType(final Callback<PaymentTypeListResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mPaymentTypeListResult = null;
        }
        if (null != DataHolder.mPaymentTypeListResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mPaymentTypeListResult);
            }
            return;
        }
        String api = "/LmReferenceServices-portlet.paymenttype/get-payment-type";
        invoke(0, api, null, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                try {
                    DataHolder.mPaymentTypeListResult = DPGsonUtil.fromJson(result.get(0).toString(), PaymentTypeListResult.class);
                    if (null != callback) {
                        callback.onSuccess(what, DataHolder.mPaymentTypeListResult);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class PaymentTypeListResult {
        private Status status;

        private List<Value> value;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(List<Value> value) {
            this.value = value;
        }

        public List<Value> getValue() {
            return this.value;
        }

        private String[] mPaymentNameArray;

        public String[] getPaymentNameArray() {
            if (null == mPaymentNameArray) {
                DPLogUtil.d("mPaymentTypeListResult " + DataHolder.mPaymentTypeListResult);
                DPLogUtil.d("mPaymentTypeListResult " + DataHolder.mPaymentTypeListResult.getValue());
                mPaymentNameArray = new String[DataHolder.mPaymentTypeListResult.getValue().size()];
                for (int i = 0; i < mPaymentNameArray.length; i++) {
                    mPaymentNameArray[i] = DataHolder.mPaymentTypeListResult.getValue().get(i).getName();
                }
            }
            return mPaymentNameArray;
        }

        public static class Value {
            private String description;

            private String name;

            private String countryCd;

            private String paymentTypeCd;

            public void setDescription(String description) {
                this.description = description;
            }

            public String getDescription() {
                return this.description;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getName() {
                return this.name;
            }

            public void setCountryCd(String countryCd) {
                this.countryCd = countryCd;
            }

            public String getCountryCd() {
                return this.countryCd;
            }

            public void setPaymentTypeCd(String paymentTypeCd) {
                this.paymentTypeCd = paymentTypeCd;
            }

            public String getPaymentTypeCd() {
                return this.paymentTypeCd;
            }

        }

        public class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }

        }
    }
}
